package com.lottery.im.module.chat.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.BaseObserver;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.constant.Constants;
import com.lottery.im.module.chat.view.GroupNoticeEditView;
import com.lottery.im.util.DateUtils;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.ext.group.TIMGroupManagerExt;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class GroupNoticeEditPresenter extends BaseFragmentPresenter<GroupNoticeEditView> {

  public GroupNoticeEditPresenter(Activity activity,
      GroupNoticeEditView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  public void groupNoticeAdd(String groupId, String content, String id) {
    ApiClient.getApiRetrofitInstance()
        .groupNoticeAdd(MyApplication.getInstance().getUserInfo().getUserId(),
            MyApplication.getInstance().getToken(), groupId, content, id)
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseObserver<Object>() {
          @Override public void onStart() {
            iView.showLoadingDialog();
          }

          @Override public void onSuccess(Object object) {
            TIMGroupManagerExt.ModifyGroupInfoParam param =
                new TIMGroupManagerExt.ModifyGroupInfoParam(groupId);
            param.setNotification(content);
            Map<String, byte[]> customInfo = new HashMap<>();
            try {
              customInfo.put(Constants.NOTICE_UPDATE_TIME,
                  DateUtils.millis2String(System.currentTimeMillis(),
                      DateUtils.DEFAULT_ALL_SDF_EMPTY)
                      .getBytes("utf-8"));
              param.setCustomInfo(customInfo);
            } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
            }
            TIMGroupManagerExt.getInstance().modifyGroupInfo(param, new TIMCallBack() {
              @Override
              public void onError(int code, String desc) {
                if (null == iView) return;
                iView.showToast("发布失败");
                iView.cancelLoadingDialog();
              }

              @Override
              public void onSuccess() {
                if (null == iView) return;
                iView.submitSuccess();
                iView.cancelLoadingDialog();
              }
            });
          }

          @Override public void onFail(String code, String msg) {
            iView.showToast(msg);
            iView.cancelLoadingDialog();
          }

          @Override public void onException(String msg) {

          }

          @Override public void onFinish() {

          }
        });
  }
}
